from microdot import Microdot,send_file
from machine import Pin
from time import time, sleep
from network import WLAN, STA_IF
 
wlan = WLAN(STA_IF)
wlan.active(True)
#注意改成自家的WiFi名称和地址
wlan.connect("-----", "-------")
print("开始连接")
while not wlan.isconnected():
    sleep(0.2)
print("连接成功")
app = Microdot()
light = Pin(2, Pin.OUT)
light.value(0)
 
@app.before_request
def request(request):
    print('HTTP请求:',request)
 
@app.route('/')
def index(request):
    return send_file('index.html')
 
@app.get('/on')
def index(request):
    light.value(1)
    return "开灯成功"
 
@app.get('/off')
def index(request):
    light.value(0)
    return "关灯成功"
 
port = 5000
print(f"请打开 http://{wlan.ifconfig()[0]}:{port}/")
app.run(host='0.0.0.0', port=port, debug=False, ssl=None)
